package com.ctg.behavior.calc.controller.userpath;

import com.alibaba.fastjson.JSON;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ctg.behavior.common.userpath.CtUserPathCalcRequest;
import com.ctg.behavior.common.userpath.CtUserPathCalcResult;

import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.InputStream;
import java.util.List;
import java.util.Random;

/*用户路径*/
@Slf4j
@RestController
@RequestMapping("/api/userpath")
public class UserPathController {

	@Value("${simulator.path}")
	private String simlutorPath;

	@PostMapping("/report")
	public CtUserPathCalcResult report(@RequestBody CtUserPathCalcRequest request) throws Exception {

		CtUserPathCalcResult result = new CtUserPathCalcResult();
		log.info("request", JSON.toJSONString(request));

		String filePath = simlutorPath + "/userpath/report/";
		FileUtils.forceMkdir(new File(filePath));

		String targetName = this.builderTargetFileName(filePath, request);
		String targetFilePath =  filePath + targetName;
		// 检查文件并读取文件
		File targetFile = new File(targetFilePath);
		if (!targetFile.exists()) {
			ClassPathResource classPathResource = null;
			if (request.getReq().getSourceType().equals("initial_event")) {
				Random random = new Random();
				if (random.nextBoolean()) {
					if (random.nextBoolean()) {
						classPathResource = new ClassPathResource("userpath/report/userpath.simple_start.json");
					}else{
						classPathResource = new ClassPathResource("userpath/report/userpath.simple_start_groupby.json");
					}
				}else{
					classPathResource = new ClassPathResource("userpath/report/userpath.simple_start_nogroup.json");
				}
			}else{
				classPathResource = new ClassPathResource("userpath/report/userpath.simple_end.json");
			}

			InputStream inputStream = classPathResource.getInputStream();
			List<String> lines = IOUtils.readLines(inputStream, "UTF-8");
			StringBuilder sbuilder = new StringBuilder();
			for (String line : lines) {
				sbuilder.append(line);
			}
			result = JSON.parseObject(sbuilder.toString(), CtUserPathCalcResult.class);
		} else {
			String resultText = FileUtils.readFileToString(targetFile, "UTF-8");
			result = JSON.parseObject(resultText.toString(), CtUserPathCalcResult.class);
		}

		log.info("result", JSON.toJSONString(result));

		return result;

	}

	public String builderTargetFileName(String filePath, CtUserPathCalcRequest request) {

		// 拼JSON文件名
		if(!request.getReq().getByFields().isEmpty()) {
			return "userpath/report/userpath.simple_start.json";
		}else {
			return "userpath/report/userpath.simple_start.json";
		}

	}

}
